package com.happyclub.lcy.dao;

import com.happyclub.pojo.TbUserRoleMap;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface IUserRole extends JpaRepository<TbUserRoleMap,String> {
    //创建用户角色中间表
    //加入驴友队，先根据队伍id查到队伍 在往用户队伍中间表,队伍角色，用户角色表里插值
    @Modifying
    @Query(value = "insert into TB_USER_ROLE_MAP(USER_ID, ROLE_ID, UPDATE_TIME, CREATE_TIME, TEAM_ID) VALUES(?1,?2,?3,?4,?5)",nativeQuery = true)
    int insertUserRoleMap(String userId, String roleId, String updateTime, String createTime, String teamId);
//退出驴友队
    @Modifying
    @Query(value = "delete from TB_USER_ROLE_MAP where USER_ID =?1 and TEAM_ID = ?2",nativeQuery = true)
    int delUserRoleMapByUserId(String userId, String teamId);
//任命副队长
    @Modifying
    @Query(value = "update TB_USER_ROLE_MAP set ROLE_ID = ?1 where USER_ID= ?2 and TEAM_ID = ?3",nativeQuery = true)
    int upUserRoleMapByUserId(String roleId, String userId, String teamId);

   /* @Modifying
    @Query(value = "select * from TB_USER_ROLE_MAP where USER_ID in (select USER_ID from TB_USER_TEAM_MAP where TEAM_ID = ?1) and ROLE_ID = ?2",nativeQuery = true)
    List<TbUserRoleMap> getAllGm(String teamId, String roleId);*/
    //查用户是否存在该队伍  也可查角色人数
    //这里有问题 为什么只能全字段名和*？其他的都报错
    @Modifying
    @Query(value = "select USER_ID, ROLE_ID, UPDATE_TIME, CREATE_TIME, TEAM_ID from TB_USER_ROLE_MAP  where USER_ID in (select USER_ID from TB_USER_TEAM_MAP where TEAM_ID = ?1) and ROLE_ID= ?2 ",nativeQuery = true)
    List<TbUserRoleMap> getUserRoleByTeamId(String teamId, String roleId);
}
